package org.core.blog.controller;

import lombok.extern.slf4j.Slf4j;
import org.core.blog.pojo.*;
import org.core.blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 获取文章列表
     */
    @GetMapping("/list")
    public Result getList(){
        List<Article> articles=articleService.getList();
        return Result.success(articles);
    }


    @GetMapping
    public Result getPage(PageQueryParam pageQueryParam){
        log.info("请求获取文章列表");
        PageResult<Article> articlesPage = articleService.getPage(pageQueryParam);
        return Result.success(articlesPage);
    }

    /**
     * 获取单篇文章
     */
    @GetMapping("/{id}")
    public Result getArticle(@PathVariable Integer id) {
        log.info("请求获取文章");
        Article article=articleService.getArticle(id);
        return Result.success(article);
    }

    /**
     * 创建文章（需管理员权限）
     */
    @PostMapping("/auth")
    public Result createArticle(@RequestBody Article article) {
        log.info("请求创建文章");
        articleService.creatArticle(article);
        return Result.success();
    }

    /**
     * 更新文章（需管理员权限）
     */
    @PutMapping("/auth")
    public Result updateArticle(@RequestBody Article article) {
        log.info("请求更改文章");
        articleService.updateArticle(article);
        return Result.success("文章更新成功");
    }

    /**
     * 删除文章（需管理员权限）
     */
    @DeleteMapping("/auth/{id}")
    public Result deleteArticle(@PathVariable Integer id) {
        log.info("请求删除文章");
        articleService.deleteArticle(id);
        return Result.success("文章删除成功");
    }

    @PostMapping("/search")
    public Result searchArticlesByParam(@RequestBody SearchParam searchParam) {
        log.info("请求按条件搜索文章: {}", searchParam);
        PageResult<Article> articlesPage = articleService.searchPage(searchParam);
        return Result.success(articlesPage);
    }
}
